package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gareth Cao
 * @date 2022/4/29 14:27
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto) {
        dishService.addByDto(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 带条件分页查询菜品信息
     */
    @GetMapping("/page")
    public R<Page<DishDto>> selectByPage(Integer page, Integer pageSize, String name) {
        //分页条件
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //查询条件
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");
        dishService.page(pageInfo, queryWrapper);

        //页面展示时需要categoryName,但是dish没有,dishDto有,响应数据需要转为Page<DishDto>
        //创建一个封装了dishDto信息的Page对象
        Page<DishDto> pageInfoDto = new Page<>();
        //pageInfo中的所有属性除了records之外全部拷贝到pageInfoDto中,使用BeanUtils工具类
        BeanUtils.copyProperties(pageInfo, pageInfoDto, "records");
        //新建一个list集合,里面存储List<DishDto>的信息
        List<DishDto> list = new ArrayList<>();
        //遍历pageInfo的records属性,将其拷贝到pageInfoDto的records属性,并添加categoryName属性
        for (Dish dish : pageInfo.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //TODO 处理分类名称
            //添加categoryName属性,根据category_id到category表查询name
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            list.add(dishDto);
        }
        //将list集合添加到pageInfoDto的records属性中
        pageInfoDto.setRecords(list);
        return R.success(pageInfoDto);
    }

    /**
     * 根据修改操作请求数据来回显数据
     */
    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id) {
        DishDto dishDto = dishService.selectById(id);
        return R.success(dishDto);
    }

    /**
     * 修改操作
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateByDishDto(dishDto);
        return R.success("修改菜品成功");
    }

    /**
     * 根据分类id查询dish表中的数据,这是后端需要的数据
     * 根据分类id需要展示dishDto数据,这是手机端需要的数据
     */
    @GetMapping("/list")
    public R<List<DishDto>> selectByCategoryId(Long categoryId) {
        //根据分类id查询dish表中的数据
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(categoryId != null, "category_id", categoryId);
        queryWrapper.eq("status", 1);
        List<Dish> dishList = dishService.list(queryWrapper);

        //手机端页面根据分类不仅需要展示菜品,也要展示口味
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //dishDto不仅需要菜品属性,还要口味
            QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
            dishFlavorQueryWrapper.eq("dish_id", dishDto.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorQueryWrapper);
            dishDto.setFlavors(dishFlavors);
            dishDtoList.add(dishDto);
        }

        return R.success(dishDtoList);
    }

}
